package com.dao.impl;

import com.core.dto.PageDTO;
import com.core.model.user.Role;
import com.dao.RoleDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/6/27
 * Time: 17:15
 * To change this template use File | Settings | File Templates.
 */
@Repository("roleDao")
public class RoleDaoImpl extends EntityDaoImpl implements RoleDao {

    @Override
    public List<Role> findAllRole(String currentPage) {
        String hql = "from " + Role.class.getName();
        Query query = currentSession().createQuery(hql);
        query.setFirstResult((Integer.parseInt(currentPage) - 1) * PageDTO.pageSize);
        query.setMaxResults(PageDTO.pageSize);
        return query.list();
    }

    @Override
    public Role findRoleByGuid(String roleGuid) {
        String hql = "from " + Role.class.getName() + " a where a.guid=:guid";
        Query query = currentSession().createQuery(hql);
        query.setParameter("guid", roleGuid);
        List<Role> list = query.list();
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

}
